﻿using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using FileSynchronizer.DataAccess;

namespace FileSynchronizer.Objects
{
    public class SyncFolderPairListHelper
    {
        #region Private variables
        //---------------------------------------------------------------------------------------------------------------------------------------
        private bool IsBusy;
        private Queue<FolderPair> myFolderPairQueue;
        private RunWorkerCompletedEventHandler myExternalCompletionHandler;
        //---------------------------------------------------------------------------------------------------------------------------------------
        #endregion

        #region Constructors
        //---------------------------------------------------------------------------------------------------------------------------------------

        public SyncFolderPairListHelper()
        {
            myFolderPairQueue = new Queue<FolderPair>();
            IsBusy = false;
        }

        public SyncFolderPairListHelper(IQueryable<FolderPair> TheFolderPairs)
        {
            myFolderPairQueue = new Queue<FolderPair>(); 
            IsBusy = false;
                        
            if (TheFolderPairs != null)
            {
                foreach (FolderPair myFolderPair in TheFolderPairs)
                {
                    if (myFolderPair.AutoSynchronize)
                    {
                        myFolderPairQueue.Enqueue(myFolderPair);
                    }
                }
            }
        }
        //---------------------------------------------------------------------------------------------------------------------------------------
        #endregion

        #region Synchronization of a FolderPair
        //---------------------------------------------------------------------------------------------------------------------------------------
        // Initiation of the Synchronization
        //---------------------------------------------------------------------------------------------------------------------------------------
        public void StartSync(RunWorkerCompletedEventHandler ExternalCompletionHandler)
        {
            if (!IsBusy)
            {
                IsBusy = true;

                myExternalCompletionHandler = ExternalCompletionHandler;

                SyncFolderPairFromQueue();
            }
        }

        //---------------------------------------------------------------------------------------------------------------------------------------
        // Perform the Synchronization
        //---------------------------------------------------------------------------------------------------------------------------------------
        private void SyncFolderPairFromQueue()
        {
            if (myFolderPairQueue.Count > 0)
            {
                FolderPair myFolderPair = myFolderPairQueue.Dequeue();
                SyncProcessor mySyncProcessor = new SyncProcessor();
                mySyncProcessor.SyncFolderPair(myFolderPair, null, SyncFolderPairCompleted);
            }
        }

        //---------------------------------------------------------------------------------------------------------------------------------------
        // Callback Function which is called whenever the synchronization of a folderpair is complete
        //---------------------------------------------------------------------------------------------------------------------------------------
        private void SyncFolderPairCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            if (myFolderPairQueue.Count > 0)
            {
                SyncFolderPairFromQueue();
            }
            else
            {
                if (myExternalCompletionHandler != null)
                {
                    myExternalCompletionHandler(sender, e);
                }
                IsBusy = false;
            }
        }

        //---------------------------------------------------------------------------------------------------------------------------------------
        #endregion

    }
}
